import BuffUtil from '@/BuffUtil';
import { ruinCongi, idMap, idMapRuins } from './config';
import BaseUtil from '@/BaseUtil';
import ArrayUtil from '@/ArrayUtil';
import PlayerUtil from '@/PlayerUtil';
export default class Ruins {
    static ruinPoints: { text: string; x: number; y: number; rect: rect; ruin?: item }[] = [];
    static ruinIds: string[] = Object.keys(idMap);
    constructor() {
        this.initConfig();
        se.onUnitPickupItem(this.action);
        se.on('怪物刷新', Ruins.delyRefrshRuins);
    }
    static delyRefrshRuins() {
        BaseUtil.runLater(5, Ruins.refreshRuins);
    }
    static refreshRuins(this: void) {
        let rs = [];
        let c = 0;
        while (c < 2) {
            let info = ArrayUtil.randomElement(Ruins.ruinPoints);
            if (rs.includes(info.rect)) continue;
            c++;
            if (info.ruin) continue;
            rs.push(info.rect);
            let id = ArrayUtil.randomElement(Ruins.ruinIds);
            info.ruin = CreateItem(id, GetRectCenterX(info.rect), GetRectCenterY(info.rect));
            PlayerUtil.message(`|cffffcc00${idMapRuins[id]?.name}|r 刷新在了 |cffdd3300${info.text}|r`);
            PingMinimap(info.x, info.y, 10);
        }
        return game_wave < game_max_wave;
    }
    action(this: void) {
        let item = GetManipulatedItem();
        let u = GetManipulatingUnit();
        let id = id2string(GetItemTypeId(item));
        if (!Ruins.ruinIds.includes(id)) return;
        if (GetUnitTypeId(u) == FourCC('ehep')) {
            UnitDropItem(u, FourCC(id));
            return;
        }
        for (let i of Ruins.ruinPoints) {
            if (i.ruin == item) {
                i.ruin = null;
            }
        }
        let buffId = idMap[id];
        if (!buffId) return;
        BuffUtil.addBuff(u, buffId, u);
        Ruins.processRuinEffect(u, id);
    }
    static processRuinEffect(u: unit, itemId: string) {
        // 圣水神符效果
        if (itemId == 'F006') {
            SetUnitLifePercentBJ(u, 100);
            SetUnitManaPercentBJ(u, 100);
        } else if ((itemId = 'F009')) {
            let life = GetUnitState(u, UNIT_STATE_LIFE);
            ShieldSystem.添加护盾值(u, 'buff_ruin_jiandun', life, false, u);
        }
    }
    initConfig() {
        for (let key in ruinCongi) {
            let value = ruinCongi[key];
            BuffUtil.registerBuffType(value);
            value.action?.();
        }
        Ruins.ruinPoints.push({ text: '北方祭坛', x: GetRectCenterX(gg_rct_ruin_n), y: GetRectCenterY(gg_rct_ruin_n), rect: gg_rct_ruin_n });
        Ruins.ruinPoints.push({ text: '南方祭坛', x: GetRectCenterX(gg_rct_ruin_s), y: GetRectCenterY(gg_rct_ruin_s), rect: gg_rct_ruin_s });
        Ruins.ruinPoints.push({ text: '东方祭坛', x: GetRectCenterX(gg_rct_ruin_e), y: GetRectCenterY(gg_rct_ruin_e), rect: gg_rct_ruin_e });
        Ruins.ruinPoints.push({ text: '西方祭坛', x: GetRectCenterX(gg_rct_ruin_w), y: GetRectCenterY(gg_rct_ruin_w), rect: gg_rct_ruin_w });
    }
}
